/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Portions created by the Initial Developer are
 * Copyright (C) 2012 the Initial Developer.
 * All Rights Reserved.
 *
 * The Original Code is this.
 *
 * The Initial Developer of the Original Code is the author below.
 */
/**
 * @file MessageQue.h
 * @brief auto generated.
 * @author K.Ozaki
 * @date 2011/10/15
 */

#ifndef MESSAGEQUE_H_
#define MESSAGEQUE_H_

#include <mqueue.h>
#include <sys/stat.h>
#include "semi/util/StringUtil.h"
#include "semi/posix/SharedMemory.h"
#include "semi/posix/SignalAction.h"
#include "semi/posix/Semaphore.h"

namespace semi {

namespace posix {

/*
 * MessageQue.h
 *
 *  @author: ozaki
 */
class MessageQue {
public:

    //    MessageQue();
    MessageQue(std::string quename);

    virtual ~MessageQue();

    void init(std::string quename);
    std::string getQueName();

    int send(std::string, unsigned int prio);

    std::string receive(unsigned int prio);

    std::string notify(unsigned int prio);

    int terminate();

    static void releaseAllMessage(); // for shut down
    static void clearCurrentQueNames(); // for start up
    static std::string toStringCurrentQue(bool bl = false);

    static std::string terminate_message; // for shut down
    static std::string error_message; // for shut down
    static std::string empty_message; // TODO

private:

    std::string QueName;

    SharedMemory mqpl;

    //    Semaphore sem;
    // static
    std::string createQuename(std::string quename);

    int addQueCount(bool send); // send/wait
    int removeQueCount(bool send); // receive
    //    int existsQueName();

    struct QueRecord {
        char queName[32]; // QueName
        int send_count; // que counter
        int wait_count; // que counter
        //        char flg; // breakmark;
    };

    struct QuePool {
        QueRecord ques[64];
    };

    static std::string que_id_file_name;

    static SignalAction* sig_action;

    //    Semaphore sem;
};

}

}

#endif /* MESSAGEQUE_H_ */
